package services

import (
	"fmt"

	"gitee.com/micro-plat/sas/sas"
	"github.com/micro-plat/hydra"
	"github.com/micro-plat/lib4go/types"
)

var euid = "hjtest"

//TestHandler .
type TestHandler struct {
}

//NewTestHandler .
func NewTestHandler() (u *TestHandler) {
	return &TestHandler{}
}

//MD5Handle .
func (o *TestHandler) MD5Handle(ctx hydra.IContext) (r interface{}) {
	client := sas.NewSasClient(euid, sas.WithSortData(), sas.WithSertModeTail())
	var params = types.XMap{
		"p1": "123456",
		"p2": "fklsdfjlskgj^^*^&",
		"p3": "受到法律看到街上分离凯瑟琳空间到沙发",
		"p4": 12.545,
		"p5": `{"j1":"sdfsd","j2":"三大类咖啡机","j3":123.5}`,
		"p6": types.XMap{"taosy": "测试"},
	}
	sign, input, err := client.MD5Sign(params)
	if err != nil {
		ctx.Log().Errorf("MD5Sign失败,err:%v", err)
		return
	}
	ctx.Log().Info("sign:", sign)
	return requestRemote(ctx, input)
}

//HmacHandle .
func (o *TestHandler) HmacHandle(ctx hydra.IContext) (r interface{}) {
	client := sas.NewSasClient(euid, sas.WithChain("&"), sas.WithKeyValue("="), sas.WithSortData(), sas.WithSertModeTail())
	var params = types.XMap{
		"p1": "123456",
		"p2": "fklsdfjlskgj^^*^&",
		"p3": "受到法律看到街上分离凯瑟琳空间到沙发",
		"p4": 12.545,
		"p5": `{"j1":"sdfsd","j2":"三大类咖啡机","j3":123.5}`,
		"p6": types.XMap{"taosy": "测试"},
	}
	_, input, err := client.HmacSign(params)
	if err != nil {
		ctx.Log().Errorf("HmacHandle失败,err:%v", err)
		return
	}
	return requestRemote(ctx, input)
}

//Sha1Handle .
func (o *TestHandler) Sha1Handle(ctx hydra.IContext) (r interface{}) {
	client := sas.NewSasClient(euid, sas.WithChain("&"), sas.WithKeyValue("="), sas.WithSortData(), sas.WithSertModeTail())
	var params = types.XMap{
		"p1": "123456",
		"p2": "fklsdfjlskgj^^*^&",
		"p3": "受到法律看到街上分离凯瑟琳空间到沙发",
		"p4": 12.545,
		"p5": `{"j1":"sdfsd","j2":"三大类咖啡机","j3":123.5}`,
		"p6": types.XMap{"taosy": "测试"},
	}
	_, input, err := client.Sha1Sign(params)
	if err != nil {
		ctx.Log().Errorf("Sha1Handle失败,err:%v", err)
		return
	}
	return requestRemote(ctx, input)
}

//Sha256Handle .
func (o *TestHandler) Sha256Handle(ctx hydra.IContext) (r interface{}) {
	client := sas.NewSasClient(euid, sas.WithChain("&"), sas.WithKeyValue("="), sas.WithSortData(), sas.WithSertModeTail())
	var params = types.XMap{
		"p1": "123456",
		"p2": "fklsdfjlskgj^^*^&",
		"p3": "受到法律看到街上分离凯瑟琳空间到沙发",
		"p4": 12.545,
		"p5": `{"j1":"sdfsd","j2":"三大类咖啡机","j3":123.5}`,
		"p6": types.XMap{"taosy": "测试"},
	}
	_, input, err := client.Sha256Sign(params)
	if err != nil {
		ctx.Log().Errorf("Sha256Handle失败,err:%v", err)
		return
	}
	return requestRemote(ctx, input)
}

//Sha512Handle .
func (o *TestHandler) Sha512Handle(ctx hydra.IContext) (r interface{}) {
	client := sas.NewSasClient(euid, sas.WithChain("&"), sas.WithKeyValue("="), sas.WithSortData(), sas.WithSertModeTail())
	var params = types.XMap{
		"p1": "123456",
		"p2": "fklsdfjlskgj^^*^&",
		"p3": "受到法律看到街上分离凯瑟琳空间到沙发",
		"p4": 12.545,
		"p5": `{"j1":"sdfsd","j2":"三大类咖啡机","j3":123.5}`,
		"p6": types.XMap{"taosy": "测试"},
	}
	_, input, err := client.Sha512Sign(params)
	if err != nil {
		ctx.Log().Errorf("Sha512Handle失败,err:%v", err)
		return
	}
	return requestRemote(ctx, input)
}

//Multi1Handle .  md5+aes
func (o *TestHandler) Multi1Handle(ctx hydra.IContext) (r interface{}) {
	client := sas.NewSasClient(euid, sas.WithChain("&"), sas.WithKeyValue("="), sas.WithSortData(), sas.WithSertModeTail(), sas.WithEncryptName("p1", "p5"))
	var params = types.XMap{
		"p1": "123456",
		"p2": "fklsdfjlskgj^^*^&",
		"p3": "受到法律看到街上分离凯瑟琳空间到沙发",
		"p4": 12.545,
		"p5": `{"j1":"sdfsd","j2":"三大类咖啡机","j3":123.5}`,
		"p6": types.XMap{"taosy": "测试"},
	}
	_, input, err := client.Multi(params, "md5", "aes", "OFB/pkcs5")
	if err != nil {
		ctx.Log().Errorf("Multi1Handle失败,err:%v", err)
		return
	}
	return requestRemote(ctx, input)
}

//Multi2Handle .  md5+des
func (o *TestHandler) Multi2Handle(ctx hydra.IContext) (r interface{}) {
	client := sas.NewSasClient(euid, sas.WithChain("&"), sas.WithKeyValue("="), sas.WithSortData(), sas.WithSertModeTail(), sas.WithEncryptName("p1", "p5"))
	var params = types.XMap{
		"p1": "123456",
		"p2": "fklsdfjlskgj^^*^&",
		"p3": "受到法律看到街上分离凯瑟琳空间到沙发",
		"p4": 12.545,
		"p5": `{"j1":"sdfsd","j2":"三大类咖啡机","j3":123.5}`,
		"p6": types.XMap{"taosy": "测试"},
	}
	_, input, err := client.Multi(params, "md5", "des", "ECB/pkcs5")
	if err != nil {
		ctx.Log().Errorf("Multi1Handle失败,err:%v", err)
		return
	}
	return requestRemote(ctx, input)
}

//Multi3Handle .  md5+Rsa
func (o *TestHandler) Multi3Handle(ctx hydra.IContext) (r interface{}) {
	client := sas.NewSasClient(euid, sas.WithChain("&"), sas.WithKeyValue("="), sas.WithSortData(), sas.WithSertModeTail(), sas.WithEncryptName("p1", "p5"))
	var params = types.XMap{
		"p1": "123456",
		"p2": "fklsdfjlskgj^^*^&",
		"p3": "受到法律看到街上分离凯瑟琳空间到沙发",
		"p4": 12.545,
		"p5": `{"j1":"sdfsd","j2":"三大类咖啡机","j3":123.5}`,
		"p6": types.XMap{"taosy": "测试"},
	}
	_, input, err := client.Multi(params, "md5", "rsa")
	if err != nil {
		ctx.Log().Errorf("Multi1Handle失败,err:%v", err)
		return
	}
	return requestRemote(ctx, input)
}

//Dynamic1Handle .  hmac+aes
func (o *TestHandler) Dynamic1Handle(ctx hydra.IContext) (r interface{}) {
	client := sas.NewSasClient(euid, sas.WithChain("&"), sas.WithKeyValue("="), sas.WithSortData(), sas.WithSertModeTail(), sas.WithEncryptName("p1", "p5"))
	var params = types.XMap{
		"p1": "123456",
		"p2": "fklsdfjlskgj^^*^&",
		"p3": "受到法律看到街上分离凯瑟琳空间到沙发",
		"p4": 12.545,
		"p5": `{"j1":"sdfsd","j2":"三大类咖啡机","j3":123.5}`,
		"p6": types.XMap{"taosy": "测试"},
	}
	_, input, err := client.Dynamic(params, "1234561254452512", "hmac", "aes", "OFB/pkcs5")
	if err != nil {
		ctx.Log().Errorf("Dynamic1Handle失败,err:%v", err)
		return
	}
	return requestRemote(ctx, input)
}

//Dynamic2Handle .  hmac+des
func (o *TestHandler) Dynamic2Handle(ctx hydra.IContext) (r interface{}) {
	client := sas.NewSasClient(euid, sas.WithChain("&"), sas.WithKeyValue("="), sas.WithSortData(), sas.WithSertModeTail(), sas.WithEncryptName("p1", "p5"))
	var params = types.XMap{
		"p1": "123456",
		"p2": "fklsdfjlskgj^^*^&",
		"p3": "受到法律看到街上分离凯瑟琳空间到沙发",
		"p4": 12.545,
		"p5": `{"j1":"sdfsd","j2":"三大类咖啡机","j3":123.5}`,
		"p6": types.XMap{"taosy": "测试"},
	}
	_, input, err := client.Dynamic(params, "88888888", "hmac", "des", "CBC/pkcs5")
	if err != nil {
		ctx.Log().Errorf("Dynamic2Handle失败,err:%v", err)
		return
	}
	return requestRemote(ctx, input)
}

//Dynamic2Handle .  hmac+des
func (o *TestHandler) CertHandle(ctx hydra.IContext) (r interface{}) {

	client := sas.NewSasClient(euid)
	resp, err := client.SecretSet(sas.CertStr, sas.WithMerchantCert("1"), sas.WithSupplierCert("2"), sas.WithRootCert("3"), sas.WithCertKey("4"))
	if err != nil {
		return err
	}

	return resp
}

//Dynamic2Handle .  hmac+des
func (o *TestHandler) GetCertHandle(ctx hydra.IContext) (r interface{}) {

	client := sas.NewSasClient(euid)
	resp, err := client.SecretGet(sas.CertStr)
	if err != nil {
		return err
	}

	return resp
}

//Dynamic2Handle .  hmac+des
func (o *TestHandler) GenerateCertHandle(ctx hydra.IContext) (r interface{}) {

	client := sas.NewSasClient(euid)
	resp, err := client.SecretGenerate(sas.CertStr,
		sas.WithMerchantCert("1"),
		sas.WithSupplierCert("2"),
		sas.WithRootCert("3"),
		sas.WithCertKey("4"))
	if err != nil {
		return err
	}

	return resp
}

func requestRemote(ctx hydra.IContext, input types.XMap) error {
	postdata := string(input.Marshal())
	ctx.Log().Debugf("请求数据:%s", postdata)
	httpClient := hydra.C.HTTP().GetRegularClient()
	content, status, err := httpClient.Request("post", "http://192.168.5.115:8362/sastest/api/sas", postdata, "utf-8", map[string][]string{"Content-Type": []string{"application/json"}})
	if err != nil || status != 200 {
		return fmt.Errorf("Post请求失败,err:%v,status:%d,content:%s", err, status, string(content))
	}

	ctx.Log().Debugf("返回数据:%s", string(content))
	return nil
}
